﻿<UserControl x:Class="BBInterfaceNET.ProjectExplorer.Views.ExplorerView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:m="clr-namespace:BBInterfaceNET.ProjectExplorer.Model"
             xmlns:cmd="clr-namespace:BBInterfaceNET.ProjectExplorer.Commands"
             xmlns:i= "clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
             xmlns:custom= "clr-namespace:BBInterfaceNET.ProjectExplorer.Behaviors"
             xmlns:conv="clr-namespace:BBInterfaceNET.ProjectExplorer.Converters"
             mc:Ignorable="d" VerticalContentAlignment="Stretch" VerticalAlignment="Stretch"
             d:DesignHeight="150" d:DesignWidth="300">
    <UserControl.Resources>
        <conv:FileTypeToImageConverter x:Key="conv"/>
        
        <HierarchicalDataTemplate DataType="{x:Type m:ProjectNode}" ItemsSource="{Binding Path=Children}">
            <StackPanel Orientation="Horizontal">
                <StackPanel.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="Add New Item" Command="{x:Static cmd:ExplorerCommands.NewFileCommand}" CommandParameter="{Binding}" ></MenuItem>
                        <MenuItem Header="Add Existing Item" Command="{x:Static cmd:ExplorerCommands.ExistingFileCommand}" CommandParameter="{Binding}" ></MenuItem>
                        <MenuItem Header="Add Directory" Command="{x:Static cmd:ExplorerCommands.NewDirectoryCommand}" CommandParameter="{Binding}" ></MenuItem>
                    </ContextMenu>
                </StackPanel.ContextMenu>
                <Image Source="/BBInterfaceNET.ProjectExplorer;component/Resources/ttProject.png" 
                       Margin="2" Width="18" VerticalAlignment="Center"></Image>
                <TextBlock Text="{Binding Path=Name}" Margin="2" VerticalAlignment="Center"></TextBlock>
            </StackPanel>
        </HierarchicalDataTemplate>
        
        <HierarchicalDataTemplate DataType="{x:Type m:DirectoryNode}" ItemsSource="{Binding Path=Children}">
            <StackPanel Orientation="Horizontal" >
                <StackPanel.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="Add New Item" Command="{x:Static cmd:ExplorerCommands.NewFileCommand}" CommandParameter="{Binding}" ></MenuItem>
                        <MenuItem Header="Add Existing Item" Command="{x:Static cmd:ExplorerCommands.ExistingFileCommand}" CommandParameter="{Binding}" ></MenuItem>

                        <MenuItem Header="Add Directory" Command="{x:Static cmd:ExplorerCommands.NewDirectoryCommand}" CommandParameter="{Binding}"></MenuItem>
                        <MenuItem Header="Delete"  Command="{x:Static cmd:ExplorerCommands.DeleteCommand}" CommandParameter="{Binding}"></MenuItem>
                        <MenuItem Header="Rename" Command="{x:Static cmd:ExplorerCommands.RenameCommand}" CommandParameter="{Binding}" ></MenuItem>
                    </ContextMenu>
                </StackPanel.ContextMenu>
                <Image Source="/BBInterfaceNET.ProjectExplorer;component/Resources/dirIconBig.png" 
                       Margin="2" Width="18" VerticalAlignment="Center"></Image>
                <StackPanel>
                    <StackPanel.Style>
                        <Style TargetType="StackPanel">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Path=Editing}" Value="false">
                                    <Setter  Property="Visibility" Value="Collapsed"></Setter>
                                </DataTrigger>
                                <DataTrigger Binding="{Binding Path=Editing}" Value="true">
                                    <Setter  Property="FocusManager.FocusedElement" Value="{Binding ElementName=txt}"></Setter>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </StackPanel.Style>
                    <TextBox Name="txt" Text="{Binding Path=Name}"
                             Margin="2" VerticalAlignment="Center">
                        <i:Interaction.Behaviors>
                            <custom:FocusBehavior/>
                        </i:Interaction.Behaviors>
                    </TextBox>
                </StackPanel>
                <StackPanel VerticalAlignment="Center" Margin="2">
                    <StackPanel.Style>
                        <Style TargetType="StackPanel">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Path=Editing}" Value="true">
                                    <Setter  Property="Visibility" Value="Collapsed"></Setter>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </StackPanel.Style>
                    <TextBlock Text="{Binding Path=Name}"></TextBlock>
                </StackPanel>
            </StackPanel>
        </HierarchicalDataTemplate>

        <HierarchicalDataTemplate DataType="{x:Type m:FileNode}">
            <StackPanel Orientation="Horizontal">
                <StackPanel.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="Open"  Command="{x:Static cmd:ExplorerCommands.OpenFileCommand}" CommandParameter="{Binding}"  ></MenuItem>
                        <MenuItem Header="Delete"  Command="{x:Static cmd:ExplorerCommands.DeleteCommand}" CommandParameter="{Binding}" ></MenuItem>
                        <MenuItem Header="Rename" Command="{x:Static cmd:ExplorerCommands.RenameCommand}" CommandParameter="{Binding}"></MenuItem>
                    </ContextMenu>
                </StackPanel.ContextMenu>
                <Image Source="{Binding Path=Name, Converter={StaticResource conv}}" 
                       Margin="2" Width="18" VerticalAlignment="Center"></Image>
                <StackPanel>
                    <StackPanel.Style>
                        <Style TargetType="StackPanel">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Path=Editing}" Value="false">
                                    <Setter  Property="Visibility" Value="Collapsed"></Setter>
                                </DataTrigger>
                                <DataTrigger Binding="{Binding Path=Editing}" Value="true">
                                    <Setter  Property="FocusManager.FocusedElement" Value="{Binding ElementName=txt}"></Setter>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </StackPanel.Style>
                    <TextBox Name="txt" Text="{Binding Path=Name}"
                             Margin="2" VerticalAlignment="Center" >
                        <i:Interaction.Behaviors>
                            <custom:FocusBehavior/>
                        </i:Interaction.Behaviors>
                    </TextBox>
                </StackPanel>
                <StackPanel VerticalAlignment="Center" Margin="2">
                    <StackPanel.Style>
                        <Style TargetType="StackPanel">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Path=Editing}" Value="true">
                                    <Setter  Property="Visibility" Value="Collapsed"></Setter>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </StackPanel.Style>
                    <TextBlock Text="{Binding Path=Name}"></TextBlock>
                </StackPanel>
            </StackPanel>
        </HierarchicalDataTemplate>
    </UserControl.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Border Background="DarkBlue">
            <TextBlock Text="Project Explorer" FontWeight="Black" Foreground="White"
                       Margin="5,2" FontSize="12"/>
        </Border>
        <TreeView  ItemsSource="{Binding Path=ProjectTree.Nodes}"
                   VerticalContentAlignment="Stretch" x:Name="theTree" 
                   Grid.Row="1">
            <i:Interaction.Behaviors>
                <custom:DoubleClickBehavior />
            </i:Interaction.Behaviors>
            <TreeView.ItemContainerStyle>
                <Style TargetType="{x:Type TreeViewItem}">
                    <Setter Property="IsExpanded" Value="true"></Setter>
                </Style>
            </TreeView.ItemContainerStyle>
        </TreeView>
    </Grid>
</UserControl>
